package com.ylfstu.jdbc.resultset;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

/**
 * @Author: Joy_yang
 * @Date: 2022/10/19 14:36
 */
public class ResultSet {
    public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {

        //获取相关的值
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        //1.注册驱动
        Class.forName(driver);
        //2.得到连接
        Connection connection = DriverManager.getConnection(url, user, password);

        //3.执行SQL
        Statement statement = connection.createStatement();
        String sql = "select id, name, sex, borndate from actor";
        //执行给定的 SQL 语句，该语句返回单个 ResultSet
        /*
            +----+-----------+-----+---------------------+
            | id | name      | sex | borndate            |
            +----+-----------+-----+---------------------+
            |  3 | 刘德华     | 男  | 1970-12-12 00:00:00 |
            |  4 | jack      | 男  | 1990-11-11 00:00:00 |
            +----+-----------+-----+---------------------+
        */
        java.sql.ResultSet resultSet = statement.executeQuery(sql);

        //使用while取出数据
        while(resultSet.next()) {
            int id = resultSet.getInt(1);//获取该行第一列数据
            String name = resultSet.getString(2);
            String sex = resultSet.getString(3);
            Date date = resultSet.getDate(4);
            System.out.println(id + "\t" + name + "\t" + sex + "\t" + date);
        }

        //4.关闭连接
        resultSet.close();
        statement.close();
        connection.close();

    }
}









